<?php

class Site extends CI_Controller {

	const DOODLES_FILE_ROOT = "/home/jiapeng/learn/fei/doodles/";
	const DOODLES_URL_ROOT = "http://www.fei.com/doodles/";

	public function index() {
		$this->load->view('site');
	}

	protected function genDoodleName($uri){
		$fileName = md5($uri) . ".png";
		return $fileName;
	}
	protected function getDoodlePath($fileName){
		return self::DOODLES_FILE_ROOT . "$fileName";
	}

	protected function getDoodleURL($fileName){
		return self::DOODLES_URL_ROOT . "$fileName";
	}

	public function saveDoodle(){
		$doodle = $GLOBALS["HTTP_RAW_POST_DATA"];
		$uri =  substr($doodle,strpos($doodle, ",") + 1);
		log_message("debug", $uri);
		$fileName = $this->genDoodleName($uri);
		$filePath = $this->getDoodlePath($fileName);
		file_put_contents($filePath, base64_decode($uri));
		$fileURL = $this->getDoodleURL($fileName);

		$service = new DoodleService();
		$service->saveDoodle("jiapeng", $fileName, $fileName);

		echo $fileURL;
	}
}
